package com.example.smsplatformapigateway.filter;

import com.example.smsplatformapigateway.cache.ParamsLocalCache;
import com.example.smsplatformapigateway.feign.CacheService;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;

/*
杨威
2022/2/13
11:36
*/
@Component
public class PublicParamFilter extends ZuulFilter {
    @Autowired
    CacheService cacheService;
    @Autowired
    private ParamsLocalCache paramsLocalCache;
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 50;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        //从本地缓存中查询必须穿的参数名称都有什么
        Set<String> publicparams =  paramsLocalCache.getPublicParamSet();
        System.out.println("publicparams = " + publicparams);

        publicparams.forEach(param->{
            String value = request.getParameter(param.toString());
            if(ObjectUtils.isEmpty(value)){
                throw new RuntimeException("参数的值为空");
                //return不能跳出lambda表达式
            }
        });


        return null;
    }
}
